
#ifdef _WIN32
#include "serial_win/Serial.h"
#else
#include "serial/Serial.h"
#endif

#include <string>

#ifdef _WIN32
#define sleep_ms(t) Sleep((t))
#else
#define sleep_ms(t) usleep((t) * 1000)
#endif

#ifdef _WIN32
#define close_fd(fd) closesocket((fd))
#else
#define close_fd(fd) close((fd))
#endif

struct CAN_DATA
{
    uint8_t cmd;
    uint8_t type;
    uint32_t id;
    uint8_t dlength;
};

void unpkg(uint8_t *buffer, uint8_t L)
{
    CAN_DATA candata;
    candata.cmd = buffer[4];
    candata.type = buffer[5];
    candata.id = (uint32_t)buffer[6] << 24 | (uint32_t)buffer[7] << 16 | (uint32_t)buffer[8] << 8 | (uint32_t)buffer[9];
    candata.dlength = buffer[10];
    uint8_t sum_chack = 0;
    for (size_t i = 2; i < L - 1; i++)
    {
        sum_chack += buffer[i];
    }

    // printf("sun=%d %d", sum_chack, (uint8_t)buffer[L - 1]);
    // printf("[");
    // for (size_t i = 0; i < L; i++)
    // {
    //     printf("%.2X ", (uint8_t)buffer[i]);
    // }
    // printf("]\n");

    if (sum_chack == (uint8_t)buffer[L - 1])
    {
        // 调用回调函数进行处理
        // if (callback != nullptr)
        //     callback(context, candata.id, candata.dlength, (uint8_t *)&buffer[11]);
        // else
        //     canCallBackfun(candata.id, candata.dlength, (uint8_t *)&buffer[11]);

        printf("id=%X\n", candata.id);
        for (size_t i = 0; i < candata.dlength; i++)
        {
            printf("%x ", (uint8_t)buffer[11 + i]);
        }
        printf("\n");
    }
}
int main()
{
    serialPort serial;

#ifdef _WIN32
    if (serial.Open(4, 460800) == false)
#else
    const char *port = "/dev/ttyUSB0";
    if (serial.Open(port, B460800) < 0)
#endif
    {
        printf("com error\n");
        exit(1);
        return 1;
    }

    uint16_t len;
    int nread;
    uint8_t b;
    uint8_t buff[200];
    std::string recvString;
    uint8_t st1 = 0x66;
    uint8_t st2 = 0xcc;
    int buff_length = 0;
    while (1)
    {
        nread = serial.ReadChar(b);
        if (nread)
        {
            if ((uint8_t)b == st1)
            {
                buff_length = 0;
                buff[buff_length++] = st1;
                while (!serial.ReadChar(b))
                {
                    sleep_ms(500);
                }
                if ((uint8_t)b == st2)
                {
                    buff[buff_length++] = st2;
                    // 帧长
                    while (!serial.ReadChar(b))
                    {
                        sleep_ms(500);
                    }
                    buff[buff_length++] = b;
                    while (!serial.ReadChar(b))
                    {
                        sleep_ms(500);
                    }
                    buff[buff_length++] = b;

                    len = (uint16_t)buff[2] << 8 | (uint16_t)buff[3];
                    if (len > 200)
                    {
                        break;
                    }
                    // 读取后续帧长
                    for (size_t i = 0; i < len; i++)
                    {
                        while (!serial.ReadChar(b))
                        {
                            sleep_ms(500);
                        }
                        buff[buff_length++] = b;
                    }
                    // pthread_create(&unpkg_thread_fd, NULL, unpakgdatathread, this);
                    // ThreadBase th(unpakgdatathread, this);
                    unpkg(buff, buff_length);
                }
                else
                {
                }
            }
            else
            {
            }
        }
        else
        {
            sleep_ms(1);
        };
    }
}